﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TestMySqlConnetPoll;
using DevExpress.XtraEditors.Repository;
using System.Threading;
using View.Common;
using DevExpress.XtraCharts;
using DevExpress.XtraEditors.Controls;

namespace View.Chart.ZNGD
{
    public partial class GYBDCCurve : DevExpress.XtraEditors.XtraForm
    {
        private GybdcQxTable gybdc = new GybdcQxTable();
        private float now_dotnum;
        public GYBDCCurve()
        {
            InitializeComponent();
            LoadDeviceList();

            (chart_dl.Diagram as XYDiagram).AxisX.WholeRange.Auto = false;
            (chart_dl.Diagram as XYDiagram).AxisX.WholeRange.AutoSideMargins = false;
            (chart_dl.Diagram as XYDiagram).AxisX.WholeRange.SetMinMaxValues(0, 10000);
            (chart_dl.Diagram as XYDiagram).AxisX.WholeRange.SideMarginsValue = 0;

            //初始化曲线名称
            IniCurveNameList();
            if (repositoryItemComboBox2.Items.Count > 0)
                this.cBoxCurveName.EditValue = (this.cBoxCurveName.Edit as RepositoryItemComboBox).Items[0];
            repositoryItemComboBox2.SelectedIndexChanged += new EventHandler(CBoxCurveName_SelectedIndexChanged);
            if (repositoryItemComboBox3.Items.Count > 0)
                this.cBoxCurveTime.EditValue = (this.cBoxCurveTime.Edit as RepositoryItemComboBox).Items[0];
            repositoryItemComboBox3.SelectedIndexChanged += new EventHandler(CBoxCurveTime_SelectedIndexChanged);
        }

        /// <summary>
        /// 加载设备列表
        /// </summary>
        private void LoadDeviceList()
        {
            foreach (string strName in c_Device.GybdcDYbx_DevList)
            {
                repositoryItemComboBox2.Items.Add(strName);
            }
        }

        private void LoadTimeList() 
        {
            gybdc.ReadTimeList((string)this.cBoxCurveName.EditValue);

            while (!gybdc.Ready_TimeList)
            {
                if (gybdc.ErrorFlag != 0)
                {
                    MessageBox.Show("查询不到对应时间列表");
                    return;
                }

                Thread.Sleep(20);
            }

            repositoryItemComboBox3.Items.Clear();

            foreach (DateTime dt in gybdc.TimeList)
            {
                ComboBoxItem item = new ComboBoxItem();

                repositoryItemComboBox3.Items.Add(UtilityFunc.DateTimeStringFormat(dt, TimeFormatType.DCGONGLV));
            }

            if (repositoryItemComboBox3.Items.Count > 0)
                this.cBoxCurveTime.EditValue = repositoryItemComboBox3.Items[0];
        }

        /// <summary>
        /// 查找数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CBoxCurveTime_SelectedIndexChanged(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            gybdc.ReadValueList((string)this.cBoxCurveName.EditValue, DateTime.Parse((string)this.cBoxCurveTime.EditValue));
            while (!gybdc.Ready_ValueList)
            {
                if (gybdc.ErrorFlag != 0)
                    return;

                Thread.Sleep(20);
            }

            ObjGybdcType objGY = gybdc.ValueList[DateTime.Parse((string)this.cBoxCurveTime.EditValue)];
            chart_dl.Series.Clear();
            strNowAxisYLable = objGY.objset.UnitName;
           
            CreateSeries((string)this.cBoxCurveName.EditValue, chart_dl, objGY.qx1, true, objGY.start1);
            CreateSeries((string)this.cBoxCurveName.EditValue, chart_dl, objGY.qx2, false, objGY.start2);
            CreateSeries((string)this.cBoxCurveName.EditValue, chart_dl, objGY.qx3, false, objGY.start3);

            (chart_dl.Diagram as XYDiagram).AxisX.WholeRange.SetMinMaxValues(0, 10000);
            (chart_dl.Diagram as XYDiagram).AxisX.VisualRange.SetMinMaxValues(0, 10000);

            (chart_dl.Diagram as XYDiagram).AxisY.WholeRange.Auto = false;
            (chart_dl.Diagram as XYDiagram).AxisY.WholeRange.SetMinMaxValues(objGY.objset.ADMin, objGY.objset.ADMax);
        }

        private string strNowAxisYLable = string.Empty;

        private void SerializationObjRCurValue()
        {
        }

        /// <summary>
        /// 添加一条曲线
        /// </summary>
        /// <param name="seriesName"></param>
        /// <param name="chart"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        private void CreateSeries(string seriesName, ChartControl chart, List<ObjRCurValue> values, bool showLegend, int strartPoint)
        {
            //初始化Series
            Series series = new Series();
            //series.ArgumentScaleType = ScaleType.DateTime;
            series.ValueScaleType = ScaleType.Numerical;
            series.Label.Border.Visible = false;
            series.Label.BackColor = Color.Yellow;
            series.View.Color = Color.Yellow;
            series.CrosshairLabelVisibility = DevExpress.Utils.DefaultBoolean.False;
            series.ChangeView(ViewType.Line);
            series.CrosshairEnabled = DevExpress.Utils.DefaultBoolean.False;
            series.CrosshairHighlightPoints = DevExpress.Utils.DefaultBoolean.False;
            series.LegendText = seriesName;
            series.ShowInLegend = showLegend;
            series.ToolTipEnabled = DevExpress.Utils.DefaultBoolean.False;

            chart.Series.Add(series);

            List<SeriesPoint> points = new List<SeriesPoint>();
            int index = strartPoint;

            foreach (ObjRCurValue qxA in values)
            {
                SeriesPoint point = new SeriesPoint(index, qxA.tValue.Value);
                index++;
                points.Add(point);
            }
            AddPoints(series, points);
        }

        /// <summary>
        /// 给一条曲线添加一组点
        /// </summary>
        /// <param name="series"></param>
        /// <param name="pointsToUpdate"></param>
        private void AddPoints(Series series, List<SeriesPoint> pointsToUpdate)
        {
            series.Points.AddRange(pointsToUpdate.ToArray());
        }

        /// <summary>
        /// 曲线名称改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CBoxCurveName_SelectedIndexChanged(object sender, EventArgs e)
        {
            gybdc.ReadTimeList((string)this.cBoxCurveName.EditValue);

            while (gybdc.Ready_TimeList)
            {
                if (gybdc.ErrorFlag != 0)
                    return;

                Thread.Sleep(20);
            }

            foreach (DateTime dt in gybdc.TimeList)
            {
                repositoryItemComboBox3.Items.Add(UtilityFunc.DateTimeStringFormat(dt, TimeFormatType.WDW));
            }
        }

        /// <summary>
        /// 初始化曲线名称列表
        /// </summary>
        private void IniCurveNameList()
        {
            //cBoxCurveName.ite
            foreach(string strkey in c_Device.GybdcDYbx_DevList)
                this.repositoryItemComboBox2.Items.Add(strkey);

        }

        private void BtnPrint_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void BtnExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void BtnClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        /// <summary>
        /// 波形测试
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnBXTest_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
        //c_RealTimeValue.GybdcqxRealValue.DevName
            if (this.btnBXTest.Caption == "波形测试")
            {
                this.btnBXTest.Caption = "暂停测试";
                this.btnPrint.Enabled = false;
                this.btnExport.Enabled = false;

                Timer_Test_Tick(null, null);
                this.timer_test.Start();
                Timer_TestCurve_Tick(null, null);
                this.timer_testCurve.Start();
            }
            else
            {
                this.btnBXTest.Caption = "波形测试";
                this.btnPrint.Enabled = true;
                this.btnExport.Enabled = true;
                this.timer_test.Stop();        
                this.timer_testCurve.Stop();
            }
        }

        private void cBoxCurveName_EditValueChanged(object sender, EventArgs e)
        {
            LoadTimeList();
            //gybdc.ReadValueList((string)this.cBoxCurveName.EditValue, DateTime.Parse((string)this.cBoxCurveTime.EditValue));
            //while (!gybdc.Ready_ValueList)
            //{
            //    if (gybdc.ErrorFlag != 0)
            //        return;

            //    Thread.Sleep(20);
            //}

            //ObjGybdcType objGY = gybdc.ValueList[DateTime.Parse((string)this.cBoxCurveTime.EditValue)];

            //CreateSeries(objGY);
        }

        /// <summary>
        /// 生成曲线
        /// </summary>
        private void CreateSeries(ObjGybdcType objGY)
        {
            chart_dl.Series.Clear();
            strNowAxisYLable = objGY.objset.UnitName;

            CreateSeries((string)this.cBoxCurveName.EditValue, chart_dl, objGY.qx1, true, objGY.start1);
            CreateSeries((string)this.cBoxCurveName.EditValue, chart_dl, objGY.qx2, false, objGY.start2);
            CreateSeries((string)this.cBoxCurveName.EditValue, chart_dl, objGY.qx3, false, objGY.start3);

            (chart_dl.Diagram as XYDiagram).AxisY.WholeRange.Auto = false;
            (chart_dl.Diagram as XYDiagram).AxisY.WholeRange.AutoSideMargins = false;
            (chart_dl.Diagram as XYDiagram).AxisY.WholeRange.SetMinMaxValues(objGY.objset.ADMin, objGY.objset.ADMax);
            (chart_dl.Diagram as XYDiagram).AxisY.WholeRange.SideMarginsValue = 0;
        }

        /// <summary>
        /// 自定义Y轴坐标文字显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Chart_Dl_CustomDrawAxisLabel(object sender, CustomDrawAxisLabelEventArgs e)
        {
            AxisBase axis = e.Item.Axis;
            if (axis is AxisY || axis is AxisY3D || axis is RadarAxisY)
            {
                //double axisValue = (double)e.Item.AxisValue;
                e.Item.Text = e.Item.Text + strNowAxisYLable;
            }

        }

        /// <summary>
        /// 每隔10秒测试一次
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Timer_Test_Tick(object sender, EventArgs e)
        {
            c_SendData.SendCmdGyqxTest((string)this.cBoxCurveName.EditValue);
        }

        /// <summary>
        /// 生成测试曲线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Timer_TestCurve_Tick(object sender, EventArgs e)
        {
            if (c_RealTimeValue.RevRealTimeGYBDCqx.ContainsKey((string)this.cBoxCurveName.EditValue))
            {
                ObjGybdcType objGY = c_RealTimeValue.RevRealTimeGYBDCqx[(string)this.cBoxCurveName.EditValue];
                if (objGY != null)
                    CreateSeries(objGY);
            }
        }

        private void cBoxCurveTime_EditValueChanged(object sender, EventArgs e)
        {

        }
    }
}